package com.emapp.lccode.activity

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.os.Bundle
import android.view.View
import android.widget.DatePicker
import androidx.fragment.app.Fragment
import com.emapp.lccode.R
import com.emapp.lccode.adapter.ViewPgerKtAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.bean.DeviceKt
import com.emapp.lccode.databinding.ActivityReportBinding
import com.emapp.lccode.fragment.ReportAddKtFragment
import com.emapp.lccode.fragment.ReportListFragment
import com.emapp.lccode.utils.MmkvUtil
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gyf.immersionbar.ktx.immersionBar
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.Calendar

/**
 * 巡检
 */
class ReportKtActivity : BaseActivity1<BaseViewModel, ActivityReportBinding>(),
    OnDateSetListener {
    var titles: ArrayList<String> = arrayListOf("巡检上报", "上报记录")
    var fragments: ArrayList<Fragment> = arrayListOf(ReportAddKtFragment.newInstance(""),
        ReportListFragment.newInstance("4"))
    private lateinit var dialog: DatePickerDialog
    private val adapter: ViewPgerKtAdapter by lazy { ViewPgerKtAdapter(this, fragments) }
    private lateinit var bundles:Bundle
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }

        mViewBind.lvTitle.tvTitle.text = "上报"
        mViewBind.lvTitle.tvRight.visibility = View.GONE
        EventBus.getDefault().register(this)
        mViewBind.viewpager.adapter = adapter
        mViewBind.viewpager.offscreenPageLimit = 2
        mViewBind.layoutTab.tabMode = TabLayout.MODE_SCROLLABLE
        TabLayoutMediator(mViewBind.layoutTab, mViewBind.viewpager) { tab, position ->
            tab.text = titles[position]
        }.attach()
        initCalendar()
    }

    override fun createObserver() {
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        MmkvUtil.putString("device","")
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.lvTitle.tvRight
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }

            else -> {}
        }
    }
    fun initCalendar() {
        val calendar = Calendar.getInstance()
        dialog = DatePickerDialog(
            this, this, calendar[Calendar.YEAR] //年份
            , calendar[Calendar.MONTH] //月份
            , calendar[Calendar.DAY_OF_MONTH]
        ) //日子
    }
    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        val desc = String.format("%s-%02d-%02d", year, month + 1, dayOfMonth)
//        if (time_flag == 1) {
//            mViewBind.tvStart.text=desc
//        } else {
//            mViewBind.tvEnd.text=desc
//        }
    }

}
